<?php

namespace Addons\Wecome\Model;

use Home\Model\WeixinModel;

/**
 * Vote模型
 */
class WeixinAddonModel extends WeixinModel {
	function reply($dataArr, $keywordArr = array()) {
		return true;
	}
	// 关注时的操作
	function subscribe($dataArr) {
		$config = getAddonConfig ( 'Wecome' ); // 获取后台插件的配置参数
		
		// 其中token和openid这两个参数一定要传，否则程序不知道是哪个微信用户进入了系统
		$param ['token'] = get_token ();
		$param ['openid'] = get_openid ();
		
		$sreach = array('[follow]', '[website]');
		$replace = array(addons_url('UserCenter://UserCenter/edit', $param), addons_url('WeiSite://WeiSite/index', $param));
		$config ['description'] = str_replace($sreach, $replace, $config ['description'] );
		
		switch ($config ['type']) {
			case '3' :
				$articles [0] = array (
						'Title' => $config ['title'],
						'Description' => $config ['description'],
						'PicUrl' => $config ['pic_url'],
						'Url' => str_replace($sreach, $replace, $config ['url'] )
				);
				$res = $this->replyNews ( $articles );
				break;
			case '2' :
				return false;
				break;
			default :
				$res = $this->replyText ( $config ['description'] );
		}
		
		return $res;
	}

    //扫码自定义二维码事件
    public function qrcode($dataArr)
    {
       // $config = getAddonConfig('Bindingfanse');
        //$url = $config['yanzhengurl'];
        $data = $dataArr['ScanCodeInfo'];
        $vale = $data['ScanResult'];
        if ($jsarray = json_decode($vale, true) && empty($jsarray['username'])) {
            $darray = json_decode(trim($vale), true);
            $yanzheng = "http://running.tpxip.com/ACE/user_doAction.do?action=checkuser&username=" . $darray['username'];
            $json = file_get_contents($yanzheng);
            $res = json_decode($json, true);
            if ($res['code']=='success') {
                $m=M('follow');
                $openid=get_openid();
                $token=get_token();
                $where['openid']=$openid;
                $where['token']=$token;
                $member=$m->where($where)->find();
                if($member['bindingstate']==1){
                    $this->replyText("您已绑定该二维码，无需重复绑定！");
                }else{
                    $member['bindingstate']=1;
                    $member['bindingvalue']=$darray['username'];
                    $result=$m->save($member);
                    if($result !== false){
                        $this->replyText("恭喜您绑定成功!");
                    }else{

                        $this->replyText("绑定信息失败！");
                    }
                }
            }else{
                $this->replyText("该二维码验证失败");
            }
        } else {
            $this->replyText("非法二维码");
        }
        return true;
    }
}
